package com.bank.basic.util;

import com.alibaba.fastjson.support.odps.udf.CodecCheck;
import com.bank.model.entity.*;
import com.bank.model.enums.*;
import com.bank.model.vo.account.AccountUpdatePasswordVo;
import com.bank.model.vo.deposit.DepositHistoryVo;
import com.bank.model.vo.deposit.DepositVo;
import com.bank.model.vo.loan.LoanManagementVo;
import com.bank.model.vo.loan.LoanVo;
import com.bank.model.vo.login.LoginVo;
import com.bank.model.vo.login.PersonalInfoVo;
import com.bank.model.vo.transfer.TransferHistoryVo;
import com.bank.model.vo.transfer.TransferVo;
import com.bank.model.vo.user.MainpageVo;
import com.bank.model.vo.user.UserVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/3/27 14:13
 */

@Component
public class VoFactory {
    DepositVo depositVo;
    AccountUpdatePasswordVo accountUpdatePasswordVo;
    LoanVo loanVo;
    TransferVo transferVo;
    UserVo userVo;
    LoanManagementVo loanManagementVo;
    DepositHistoryVo depositHistoryVo;
    TransferHistoryVo transferHistoryVo;

    void initDepositVo(){
        this.depositVo = new DepositVo();
    }
    public DepositVo getDepositVo(Deposit deposit){
        initDepositVo();
        this.depositVo.setId(deposit.getId());
        this.depositVo.setUserId(deposit.getUserId());
        this.depositVo.setAccountId(deposit.getAccountId());
        this.depositVo.setStatus(DepositStatusEnum.gertName(deposit.getStatus()));
        this.depositVo.setType(DepositTypeEnum.gertName(deposit.getType()));
        this.depositVo.setMoney(Double.parseDouble(deposit.getMoney()));
        this.depositVo.setInterestRate(Double.parseDouble(deposit.getInterestRate()));
        this.depositVo.setDuration(Integer.parseInt(deposit.getDuration()));
        if(deposit.getType() == 1){
            this.depositVo.setLatestTime(deposit.getUpdatedTime());
        }else{
            this.depositVo.setLatestTime(deposit.getCreatedTime());
        }
        return this.depositVo;
    }
    void initAccountUpdatePasswordVo(){
        this.accountUpdatePasswordVo = new AccountUpdatePasswordVo();
    }
    public AccountUpdatePasswordVo getAccountUpdatePasswordVo(String accountId){
        initAccountUpdatePasswordVo();
        this.accountUpdatePasswordVo.setAccountId(accountId);
        return this.accountUpdatePasswordVo;
    }
    void initLoanVo(){
        this.loanVo = new LoanVo();
    }
    public LoanVo getLoanVo(Loan loan){
        initLoanVo();
        this.loanVo.setId(loan.getId());
        this.loanVo.setBorrowId(loan.getBorrowId());
        this.loanVo.setApproverId(loan.getApproverId());
        this.loanVo.setTotalMoney(Double.parseDouble(loan.getTotalMoney()));
        this.loanVo.setPaidMoney(Double.parseDouble(loan.getPaidMoney()));;
        this.loanVo.setInterestRate(Double.parseDouble(loan.getInterestRate()));
        this.loanVo.setType(LoanTypeEnum.gertName(loan.getType()));
        this.loanVo.setStatus(LoanStatusEnum.gertName(loan.getStatus()));
        this.loanVo.setCreatedTime(loan.getCreatedTime());
        return this.loanVo;
    }
    public List<LoanVo> getLoanVos(List<Loan> loans){
        List<LoanVo> loanVos = new ArrayList<>();
        for(Loan loan:loans){
            loanVos.add(getLoanVo(loan));
        }
        return loanVos;
    }
    void initTransferVo(){
        this.transferVo = new TransferVo();
    }
    public TransferVo getTransferVo(Transfer transfer,String accountId){
        initTransferVo();
        this.transferVo.setId(transfer.getId());
        this.transferVo.setFromId(transfer.getFromId());
        this.transferVo.setToId(transfer.getToId());
        this.transferVo.setMoney(Double.parseDouble(transfer.getMoney()));
        this.transferVo.setServiceCharge(Double.parseDouble(transfer.getServiceCharge()));;
        this.transferVo.setType(TransferTypeNum.gertName(transfer.getType()));
        this.transferVo.setStatus(TransferStatusNum.gertName(transfer.getStatus()));
        this.transferVo.setCreatedTime(transfer.getCreatedTime());
        this.transferVo.setFinishedTime(transfer.getCreatedTime());
        if(accountId.equals(transfer.getFromId().toString())) this.transferVo.setIfSelf(true);
        else this.transferVo.setIfSelf(false);
        return this.transferVo;
    }
    void initUserVo(){
        this.userVo = new UserVo();
    }
    public UserVo getUserVo(User user){
        initUserVo();
        this.userVo.setId(user.getId());
        this.userVo.setUsername(user.getUsername());
        this.userVo.setPhoneNumber(user.getPhoneNumber());
        this.userVo.setSex(user.getSex());
        this.userVo.setAge(user.getAge());
        this.userVo.setStatus(StatusEum.gertName(user.getStatus()));
        this.userVo.setBirthdate(user.getBirthdate());
        this.userVo.setHeadPortrait(user.getHeadPortrait());
        this.userVo.setIdentityNumber(user.getIdentityNumber());
        this.userVo.setHeadPortrait(user.getHeadPortrait());
        this.userVo.setCreatedTime(user.getCreatedTime());
        this.userVo.setUpdatedTime(user.getUpdatedTime());
        return this.userVo;
    }
    void initLoanManagementVo(){
        this.loanManagementVo = new LoanManagementVo();
    }
    public LoanManagementVo getLoanManagementVo(Loan loan,User user){
        initLoanManagementVo();
        this.loanManagementVo.setLoanVo(getLoanVo(loan));
        this.loanManagementVo.setUserVo(getUserVo(user));
        return this.loanManagementVo;
    }
    public LoanManagementVo getLoanManagementVo(Loan loan,UserVo userVo){
        initLoanManagementVo();
        this.loanManagementVo.setLoanVo(getLoanVo(loan));
        this.loanManagementVo.setUserVo(userVo);
        return this.loanManagementVo;
    }
    public List<LoanManagementVo> getLoanManagementVos(UserVo userVo,List<Loan> loans){
        List<LoanManagementVo> loanManagementVos = new ArrayList<>();
        for(Loan loan:loans){
            loanManagementVos.add(getLoanManagementVo(loan,userVo));
        }
        return loanManagementVos;
    }
    void initDepositHistoryVo(){
        this.depositHistoryVo = new DepositHistoryVo();
    }
    public DepositHistoryVo getDepositHistoryVo(DepositVo depositVo,UserVo userVo){
        initDepositHistoryVo();
        this.depositHistoryVo.setDepositVo(depositVo);
        this.depositHistoryVo.setUserVo(userVo);
        return this.depositHistoryVo;
    }
    public List<DepositHistoryVo> getDepositHistoryVos(UserVo userVo,List<DepositVo> depositVos){
        List<DepositHistoryVo> depositHistoryVos = new ArrayList<>();
        for(DepositVo depositVo:depositVos){
            depositHistoryVos.add(getDepositHistoryVo(depositVo,userVo));
        }
        return depositHistoryVos;
    }
    public List<DepositHistoryVo> getDepositHistoryVos(User user,List<Deposit> deposits){
        List<DepositHistoryVo> depositHistoryVos = new ArrayList<>();
        for(Deposit deposit:deposits){
            depositHistoryVos.add(getDepositHistoryVo(getDepositVo(deposit),getUserVo(user)));
        }
        return depositHistoryVos;
    }
    void initTransferHistoryVo(){
        this.transferHistoryVo = new TransferHistoryVo();
    }
    public TransferHistoryVo getTransferHistoryVo(TransferVo transferVo,UserVo userVo){
        initTransferHistoryVo();
        this.transferHistoryVo.setTransferVo(transferVo);
        this.transferHistoryVo.setUserVo(userVo);
        return this.transferHistoryVo;
    }
    public List<TransferHistoryVo> getTransferHistoryVos(UserVo userVo,List<TransferVo> transferVos){
        List<TransferHistoryVo> transferHistoryVos = new ArrayList<>();
        for(TransferVo transferVo:transferVos){
            transferHistoryVos.add(getTransferHistoryVo(transferVo,userVo));
        }
        return transferHistoryVos;
    }
}